const { loginStorage } = require("../../actions/login");
import {Storage} from "../../utils/storage";
import {api} from "../../config/api";
import {constant} from "../../types/constant";
const md5 = require('../../utils/md5.js');
const Core = require('../../../node_modules/zk-mini-core/miniprogram_dist/index');
const App = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    
  },

  /**
   * 组件的初始数据
   */
  data: {
    account: 1,
    username:"",
    password:"",
    tel:"",
    telCode:"",
    accountLoginValidate:false,
    reminderShow:true,
    cloudPic: constant.cloudIconUrl,
    menuList:[],
    moduleList: [],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    updateTel(e){
      
      this.setData({
        timeCountDownTop: e.detail.value
      })
    },

    checkoutAccount(){
      this.setData({
        account: this.data.account==1?2:1
      })
    },
    usernameTest(e){
      let username=e.detail.value
      this.setData({
        username: username
      })
    },
    passwordTest(e){
      let password=e.detail.value
      this.setData({
        password: password
      })
    },
    inputTel(e){
      let tel=e.detail.value    
      this.setData({
        tel: tel
      })
    },
    telCodeTest(e){
      let telCode=e.detail.value  
      this.setData({
        telCode: telCode
      })
    },
    login(){
      console.log('点击了登录按钮')
      this.accountLogin()

    },
    async accountLogin(){
      let valRes = await Core.validateRequest({'validateType':'validateTel','validateData':this.data.username})
      console.log('phone',this.data.username)
      console.log('---',JSON.stringify(valRes))
      if (!valRes.state){
        switch(valRes.type){
          case 0:{
            this.message('温馨提示：用户名和密码不能为空！')
          }
          break;
          case 1:{
            this.message('温馨提示：用户名必须是11位数字！')
          }
          break;
        }
        
      }else{
        this.setData({
          username: this.data.username,
          password: this.data.password,
        }),
          this.loginAction();
      }
    },
    async loginAction() {
      wx.showLoading({ title: '正在登录...', mask: true })
      let url = api.loginUrl.getUserInfoText;
      //发起登陆request
      const userInfo = await Core.httpRequest('POST',url,{phoneNumber:this.data.username,password:md5.hex_md5(this.data.password)});
      
      console.log('userInfo',JSON.stringify(userInfo))
      if (!userInfo.success) {
      }else{
      
      let menus = Storage.get('menusData');
      console.log('menusData',menus)
      if(menus!=null&&menus!=undefined){
        this.setData({
          moduleList: JSON.parse(menus)
        })
      }
      let menuArr = [];
      this.data.moduleList.forEach(ele=>{
        userInfo.data.appMenus.forEach(e=>{
          if (e===ele.permission){
            menuArr = menuArr.concat(ele)
          }
        })
      })
      let url = '/miniprogram_npm/npm_base/pages/home/index?menuArr=' + JSON.stringify(menuArr)
      wx.navigateTo({
        url: url,
      })
      }
      
      wx.hideLoading()
      this.controlLogin(userInfo)
    },
    controlLogin(userInfo) {
      //缓存必要的信息
      if (userInfo.ret == 'ok') {
        let menuMap = {
        }
        userInfo.data.appMenus.forEach(function(value,index){
          value = value.replace(/:/g, '')
          menuMap[value] = "true"
        })
        Storage.put('menuMap',menuMap)
        loginStorage.signIn(userInfo)
      }else{
        this.message(userInfo.msg)
      }
    },
    message(msg){
      wx.showToast({
        title: msg,
        icon: 'none',
        duration: 2000
      })
    }
  },
  ready() {
    this.setData({
      moduleList: App.globalData.moduleList
    })
  }
})